//
//  YCCollectionViewController.m
//  YCToolkit
//
//  Created by 蔡亚超 on 2018/7/20.
//  Copyright © 2018年 WellsCai. All rights reserved.
//

#import "YCCollectionViewController.h"
#import "YCCollectionViewCell.h"
#import <UIImageView+WebCache.h>
#import "GKPhotoBrowser.h"

@interface YCCollectionViewController()<GKPhotoBrowserDelegate>
@property(nonatomic,strong)NSArray      *arr1;
@end

@implementation YCCollectionViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.arr1 = @[@"http://h.hiphotos.baidu.com/zhidao/pic/item/b3b7d0a20cf431ad6ceeb36a4d36acaf2edd98a1.jpg",
                  @"http://h.hiphotos.baidu.com/zhidao/pic/item/a50f4bfbfbedab644495da7cfc36afc378311ea2.jpg",
                  @"http://a.hiphotos.baidu.com/zhidao/pic/item/7e3e6709c93d70cf0b741421fcdcd100baa12b0b.jpg",
                  @"http://g.hiphotos.baidu.com/zhidao/pic/item/8b82b9014a90f6035dd466133112b31bb051ed7a.jpg",
                  @"http://h.hiphotos.baidu.com/zhidao/pic/item/86d6277f9e2f0708dff00629e124b899a901f226.jpg",
                  @"http://f.hiphotos.baidu.com/zhidao/pic/item/060828381f30e9241d12d97f4a086e061c95f7ce.jpg"];
    
    [self setCollectionView];
}

- (void)setCollectionView {
    HWWeakSelf
    [self.collectionView hw_registerCell:[YCCollectionViewCell class]];
    [self setCellForRowBlock:^UICollectionViewCell * _Nonnull(NSIndexPath * _Nonnull indexPath) {
        return [weakSelf getYCCollectionViewCell:indexPath];
    } cellSizeBlock:^CGSize(NSIndexPath * _Nonnull indexPath) {
        CGFloat width = ([UIScreen mainScreen].bounds.size.width - 30)/3;
        return CGSizeMake(width, width);
    }];
}
- (UICollectionViewCell *)getYCCollectionViewCell:(NSIndexPath *)indexPath {
    YCCollectionViewCell *cell = [self.collectionView hw_dequeueReusableCell:[YCCollectionViewCell class] and:indexPath];
    [cell.imageView sd_setImageWithURL:self.arr1[indexPath.row] placeholderImage:[UIImage imageNamed:@"timg"]] ;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arr1.count;
}

#pragma mark <UICollectionViewDelegate>
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *photos = [NSMutableArray new];
    [self.arr1 enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        GKPhoto *photo = [GKPhoto new];
        photo.url = [NSURL URLWithString:obj];
        photo.placeholderImage = [UIImage imageNamed:@"timg"];
        [photos addObject:photo];
    }];
    [self showPhotoBrowser:photos currentIndex:indexPath.item];
}
- (void)showPhotoBrowser:(NSArray<GKPhoto *> *)photos currentIndex:(NSInteger)index {
    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:index];
    browser.showStyle = GKPhotoBrowserShowStyleZoom;        // 缩放显示
    browser.hideStyle = GKPhotoBrowserHideStyleZoomScale;   // 缩放隐藏
    browser.loadStyle = GKPhotoBrowserLoadStyleIndeterminateMask; // 不明确的加载方式带阴影
    browser.maxZoomScale = 2.0f;
    browser.doubleZoomScale = 2.0f;
    browser.isHideSourceView = NO;
    browser.isResumePhotoZoom = YES;
    browser.delegate = self;
    browser.isShowSave = YES; // 是否显示保存按钮(记得给权限)
    [browser showFromVC:self];
}
//MARK: - GKPhotoBrowserDelegate
- (UIImageView *)photoBrowser:(GKPhotoBrowser *)browser imageViewWithIndex:(NSInteger)index {
    YCCollectionViewCell *cell = (YCCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    if (!cell) {
        return [UIImageView new];
    }
    return cell.imageView;
}

@end
